/**************************************************************************
	Replication do-file: "Corruption in Customs"
	Cyril Chalendard, Ana Fernandes, Gael Raballand and Bob Rijkers
	
	Created on: 01/07/2022
**************************************************************************/

clear all
set more off, perm
cap log close
cls

* ----------------------- DIRECTORIES AND FOLDERS ----------------------- *

* Directories in which data are stored
global main "PUT YOUR DIRECTORY PATH HERE"
cd "$folder"

* Folders
global outputdata = "$main\Output Data"
cap mkdir "$main\Tables"
global tables = "$main\Tables"

* ----------------------------- BEGINS HERE ----------------------------- *

* -------
* Dataset
* -------
use "$outputdata\CFRR.dta", clear // open dataset

* Sample
keep if sample_int==1 & eis_f!=. // keep observations of interest

* Globals
global match "risk tax_rate red_i mixed_dec differentiated valitrade_advice ln_iwei ln_ival_usd"

* Mathing
set seed 1234
qui: psmatch2 sig_c_f $match if sample_reg==1 // matching based on risk characteristics
gen sample_match = . // create variable to identify matches
levelsof _n1 , local(pairs) // group according to the first nearest neighbor and store these in a local
foreach i in `pairs' {
	replace sample_match = 1 if _id==`i'
} // Loop: replace match if the psmatch id equals the nearest neighbor group value

* Sample
keep if sample_match==1 | _n1!=. // keep sample of pairs


* ---------
* Table A11
* ---------

* Matrix
mat tableA11 = J(8,6,.)
mat tableA11_T1 = J(8,1,.)
mat tableA11_T2 = J(8,1,.)

* Coefficients
local i = 1
local j = 1
foreach x in $match {

	qui: ttest `x' if sample_reg==1 , by(sig_c_f)
	
	mat tableA11[`i',`j'] = r(mu_2)
	mat tableA11_T1[`i',1] = r(N_2)
	local j = `j' + 2
	mat tableA11[`i',`j'] = r(mu_1)
	mat tableA11_T2[`i',1] = r(N_1)
	local j = `j' + 2
	mat tableA11[`i',`j'] = r(mu_2)-r(mu_1)
	local j = `j' + 1
	mat tableA11[`i',`j'] = r(p)
	local i = `i' +1
	local j = `j' -5
}


* Export
* ------
putexcel set "$tables\Table A11.xlsx", replace sheet(tableA11) // create a new excel spreadsheet

* Main titles
putexcel (B1:C1), merge hcenter vcenter bold
putexcel B1 = "Treated declarations"
putexcel (D1:E1), merge hcenter vcenter bold
putexcel D1 = "Control declarations"
putexcel F1 = "Difference", hcenter vcenter bold
putexcel G1 = "P-value", hcenter vcenter bold

* Secondary titles
putexcel B2 = "Average", hcenter vcenter bold
putexcel C2 = "Observations", hcenter vcenter bold
putexcel D2 = "Average", hcenter vcenter bold
putexcel E2 = "Observations", hcenter vcenter bold
putexcel G2 = "(two-sided test)", hcenter vcenter bold

* Row names
local j = 3
local vars `" "Risk score" "Tax rate" "Red channel dummy" "Mixed shipment dummy" "Differentiated share" "Valuation advice dummy" "Log initial weight" "Log initial value" "'
forvalues i = 1/8 {
	local x: word `i' of `vars'

	putexcel A`j' = "`x'", left vcenter
	local j = `j'+1
}

* Coefficients
putexcel B3 = matrix(tableA11), nformat(0.000) hcenter vcenter
putexcel C3 = matrix(tableA11_T1), nformat(number_sep) hcenter vcenter
putexcel E3 = matrix(tableA11_T2), nformat(number_sep) hcenter vcenter


* -------------------------------- ENDS HERE -------------------------------- *